#include "prime.hh"

ADistributedModule *factorial_module_maker()
{
    return (new Prime(""));
}


Prime::Prime(string modulePath)
    : ADistributedModule(modulePath, FACTORIAL_MODULE_MAKER_SYMBOL)
{
}

Prime::~Prime()
{
}

bool Prime::LoadData(void *data)
{
    _value = *(double*)(data);
    return (true);
}

bool Prime::ExportData(void **data, int *size)
{
    *data = (void*)(&_value);
    *size = sizeof(double);
    return (true);
}

void Prime::SetValue(double value)
{
    _value = value;
}

double Prime::GetResult()
{
    return (_value);
}

bool Prime::executeLocal()
{
    double value;
    int i;
    int j;

    value = _value;
    for (i = 1; value > 0; ++i)
    {
        for (j = 2; j < i; ++j)
            if (i % j == 0)
                break;
        if (i == j)
            value--;
    }
    // i is the value-eme prime number!!!
    _value = i-1;

    return (true);
}
